



//
//  BaseSearchView.m
//  住房条件帅选器
//
//  Created by MB__Lee on 2018/5/18.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "BaseSearchView.h"


#define TopBarHeight  _OriginalHeight

@interface BaseSearchView()<AreaTableviewDelegate,MBBTNTableviewDelegate,BaseTypeTableVeiwDelegate,PriceTableviewDelegate>


@property(nonatomic,strong)UIView     *ARTableview;//区域对应的 视图
@property(nonatomic,strong)UIView     *PRTableview;
@property(nonatomic,strong)UIView     *RTTableview;
@property(nonatomic,strong)UIView     *MRTableview;



@property(nonatomic,strong)BaseHousePostModel *lastModel;//记录上一次请求的模型

@property(nonatomic,strong)UIButton *lastSelectedBTN;//上一次被选中的按钮
@property(nonatomic,assign)CGFloat OriginalHeight; //原来的高度

@property(nonatomic,strong)NSArray *btnMut;//存放按钮名称

@end


@implementation BaseSearchView


-(instancetype)initWithFrame:(CGRect)frame btnMut:(NSArray *)btnMut{
    BaseSearchView *base = [[[self class] alloc]initWithFrame:frame];
    
    base.btnMut = btnMut;
    [base MBMCreatFourButton];
    return  base;
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    //设置默认frame
    //    CGRect realFrame =  CGRectMake(0, MBScale(20), self.bounds.size.width, MBScale(25));
    if (self  = [super initWithFrame: frame]) {
        //生成上下分割线
        [self MBCreatUpDownLines];
        
        // 记录最初的高度
        _OriginalHeight = frame.size.height;
        
        //初始化网络请求数据model
        _postModel = [[BaseHousePostModel alloc]init];
//        _postModel.category = @"1001";//默认是住宅类型；
      
        
    }
    return  self;
}

-(void)MBCreatUpDownLines{
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 1)];
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1)];
    line1.backgroundColor = MB_RGBA(242, 242, 242, 1.0);
    line2.backgroundColor = MB_RGBA(242, 242, 242, 1.0);
    [self addSubview:line1];
    [self addSubview:line2];
    
}
-(void)MBMCreatFourButton{
    
    CGFloat btnWidth = self.bounds.size.width * 0.25;
    //    CGFloat btnHeight = MBScale(25);
    CGFloat btnHeight = self.bounds.size.height-1;
    
    for (int i = 0; i< 4; i++) {
        MSButton *button  = [MSButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(btnWidth * i, 0, btnWidth, btnHeight);
        [self addSubview:button];
        
        [button setTitle:self.btnMut[i] forState:UIControlStateNormal];
        [button setTitleColor:MB_mainColor forState:UIControlStateNormal];
        [button setTitleColor:MB_mainSelectedColor forState:UIControlStateSelected];
        button.backgroundColor = [UIColor whiteColor];
        button.titleLabel.font = MBFont(14);
        
        
        
        
        button.tag = 100 + i;
        [button addTarget:self action:@selector(MSButtonClik:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    
}

#pragma mark --四个按钮的点击事件
-(void)MSButtonClik:(UIButton *)btn{
    //设置对应视图
    [self setInstanceToView];
    
    
    NSInteger num = btn.tag - 100;
    //判断本次点击的按钮是不是上一次被选中的按钮
    if (btn.tag != _lastSelectedBTN.tag) {
        //移除按钮对应的视图移除掉
        [self removeSubviewWithTag:_lastSelectedBTN.tag - 100];
        _lastSelectedBTN.selected = NO;
    }
    
    _lastSelectedBTN = btn;
    btn.selected = !btn.selected;
    
    //根据按钮状态移除或者添加视图
    if (num == 0 ) {
        [self addOrRemoveWithBTN:btn view:self.ARTableview];
        
    }else if (num == 1){
        [self addOrRemoveWithBTN:btn view:self.PRTableview];
        
    }else if (num == 2){
        [self addOrRemoveWithBTN:btn view:self.RTTableview];
        
    }else if (num == 3){
        [self addOrRemoveWithBTN:btn view:self.MRTableview];
    }
    
    
}

// 设置对应视图
-(void)setInstanceToView{
    _ARTableview = self.viewArray[0];
    _PRTableview = self.viewArray[1];
    _RTTableview = self.viewArray[2];
    _MRTableview = self.viewArray[3];
}

//按钮为选中状态显示大图，并且添加子视图；按钮为未选中状态的时候，移除子视图，仅显示搜索框概况
-(void)addOrRemoveWithBTN:(UIButton *)btn view:(UIView *)targetView{
    CGRect frame = self.frame;
    if (btn.selected) {
        
        // 1.按钮为选中状态的时候，添加视图；
        [self addSubview:targetView];
        
        //根据不同的子视图来设置不同的高度
        frame.size.height = SCREEN_HIGHT - TopBarHeight;
        self.frame = frame;
    }else{
       //2.按钮为未选中的状态是，移除视图
        [self resetFrame:targetView];
        
        
    }

}

-(void)removeSubviewWithTag:(NSInteger)tag{
    if (tag == 0) {
        [self.ARTableview removeFromSuperview];
    }else if (tag == 1){
        [self.PRTableview removeFromSuperview];
    }else if (tag == 2){
        [self.RTTableview removeFromSuperview];
    }else if (tag == 3){
        [self.MRTableview removeFromSuperview];
    }
    
    
}
#pragma mark --移除视图，恢复原来状态

-(void)resetFrame:(UIView *)targetView{ // 仅仅是移除视图
    _lastSelectedBTN.selected = NO;
    [targetView removeFromSuperview];
    CGRect frame = self.frame;
    frame.size.height = _OriginalHeight;
    self.frame = frame;
}
-(void)resetFrameAndRemoveView:(UIView *)targetView{ //移除视图并给代理发送消息
    
    _lastSelectedBTN.selected = NO;
    [targetView removeFromSuperview];
    CGRect frame = self.frame;
    frame.size.height = _OriginalHeight;
    self.frame = frame;
    
    
//    if (_postModel == _lastModel) {
//
//        NSLog(@"111---");
//        return;
//    }
    
    if ([self.delegate respondsToSelector:@selector(SearchView:sendPostModel:)]) {
        [self.delegate SearchView:self sendPostModel:_postModel];
        _lastModel = _postModel;
        
    }
    
}
//点击空白返回
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self MSButtonClik:_lastSelectedBTN];
}
#pragma mark --- 子视图的代理方法 ---必须让子类去调用
//1/选择区域的返回数据

-(void)AreaTableview:(AreaTableview *)areTable sendDistriceInfo:(NSString *)disctricName regionName:(NSString *)regionName{
    
    _postModel.district = disctricName;
    _postModel.region = regionName;
    
    [self resetFrameAndRemoveView:areTable];
}

- (void)AreaTableview:(AreaTableview *)areTable sendDistriceModel:(ArDistrictModel *)disctricName regionModel:(ArRegionModel *)regionName {
    
    if (regionName == nil) {
        _postModel.district = disctricName.code;
        _postModel.region = @"";
    }else{
        _postModel.district = disctricName.code;
        _postModel.region = regionName.code;
    }

    
    [self resetFrameAndRemoveView:areTable];
}
//2、租金
-(void)priceTableview:(PriceTableview *)priceTableview priceModel:(PriceModel *)priceModel{
    _postModel.rent_end = priceModel.value_end;
    _postModel.rent_start = priceModel.value_start;
    
    [self resetFrameAndRemoveView:priceTableview];
}


//2、--- 特色      ---使用       ----户型使用
-(void)BaseTypeTableVeiw:(BaseTypeTableVeiw *)tableview sendSelectedIndexPathString:(NSString *)contentString{
    
    if ([tableview isKindOfClass:[FootViewTableView class]]) { //户型
        _postModel.bedroom = contentString;
    }else if ([tableview isKindOfClass:[BaseTypeTableVeiw class]]){ //特色
        _postModel.characteristic = contentString;
    }
     [self resetFrameAndRemoveView:tableview];
}

//3、--- 户型      ---使用
-(void)BaseTypeTableVeiw:(BaseTypeTableVeiw *)tableview sendMin:(NSString *)min max:(NSString *)max{
    _postModel.square_start = min;
    _postModel.square_end   = max;
    
       [self resetFrameAndRemoveView:tableview];
}
//4、5、--- 面积 ---使用 --根据模型里面的 属性值来判断
-(void)BaseTypeTableVeiw:(BaseTypeTableVeiw *)tableview sendModel:(BaseModel *)model{
    
        _postModel.rent_start = model.value_start;
        _postModel.rent_end = model.value_end;
    
       [self resetFrameAndRemoveView:tableview];
    
}
//6、--更多
-(void)MBBTNTableview:(MBBTNTableview *)tableview sendIndexPathArray:(NSMutableArray<NSIndexPath *>*)indexPathMut{
    for (NSIndexPath *indexPath in indexPathMut) {
        if (indexPath.section == 0) {
            
            NSString *name = [tableview.mut[0] objectAtIndex:indexPath.row];
            _postModel.characteristic = name;
        }else if (indexPath.section == 1){
            NSString *decoration = [tableview.mut[1] objectAtIndex:indexPath.row];
            _postModel.decoration = decoration;
            
        }else if (indexPath.section == 2){
            NSString *orientation = [tableview.mut[2] objectAtIndex:indexPath.row];
            _postModel.orientation = orientation;
            
        }else if (indexPath.section == 3){
//            NSString *d_floor = [tableview.mut[3] objectAtIndex:indexPath.row];
            //设置楼层---没有属性-
            
            
            
        }
    }
    
       [self resetFrameAndRemoveView:tableview];
    
}
-(void)MBBTNTableviewClearInfo:(MBBTNTableview *)tableview{
    
    _postModel.characteristic = @"";
    _postModel.decoration = @"";
    _postModel.orientation = @"";
    
}


#pragma mark ----方法实现

//让子类去设置类型
-(void)MB_setPostModelCategory:(BaseHousePostModel *)model{
    
}
//让子类去实现
-(void)setViewArray:(NSMutableArray<UIView *> *)viewArray{
    _viewArray = viewArray;
}




@end
